home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 121_01 / if.c < prev    next >
Text File  |  1985-08-19  |  3KB  |  137 lines

  1. /*
  2. HEADER: CUG 121.??;
  3.  
  4.     TITLE:    If - conditionally execute coommands;
  5.     VERSION:    1.0;
  6.     DATE:    09/01/85;
  7.     DESCRIPTION: "This program executes any other program (passing command
  8.         line arguments if specified) when a conditional test is met.
  9.         The following conditions may be tested:
  10.             file exists/does not exist
  11.             file is read-only/read-write
  12.             string arguments are equal/unequal.";
  13.     KEYWORDS:    conditional, command;
  14.     SYSTEM:    CP/M;
  15.     FILENAME:    IF.C;
  16.     WARNINGS:    "Copyright (c) 1982, Steve Blasingame.
  17.         Requires local.c for link.
  18.         You can't run any builtin (CCP) commands from IF.";
  19.     SEE-ALSO:    SH.C;
  20.     AUTHORS:    Steve Blasingame;
  21.     COMPILERS:    BDS-C 1.50;
  22. */
  23. /********************************************************
  24.  *    IF - Execute Commands Conditionally
  25.  ********************************************************
  26.  
  27.  Description
  28.     This thing is based very roughly on the V6 Unix
  29.     'IF' Command.  It accepts the following types
  30.     of conditional expressions:
  31.  
  32.     if -r file command args        (if readable)
  33.     if -w file command args        (if writeable)
  34.     if !(-r) file command args    (if not found)
  35.     if !(-w) file command args    (if readonly)
  36.     if string1 == string2 command args
  37.     if string1 != string2 command args
  38.  
  39.  
  40.     Obviously there is much room for improvement,
  41.     but consider what is underneath this stuff.
  42.     If you have suggestions or FIXUPS please
  43.     contact me at one of the following places.
  44.  
  45.  
  46.     Usenet addresses;
  47.                 duke!uok!bsteve (ECN Unix)
  48.                 duke!uok!ishtar!bsteve (cnet)
  49.  
  50.     Cnode Ishtar;        (405)364-1373 Weekdays
  51.                 mail to bsteve or unix,bsteve
  52.  
  53. */
  54.  
  55. #include <bdscio.h>
  56. #include "fcb.h"    /* structure of a CP/M fcb */
  57.  
  58. #define    NOTFOUND    -1
  59. #define    READWRITE    0
  60. #define    READONLY    1
  61.  
  62. int    doshell();
  63. int    gripe();
  64. int    fstat();
  65.  
  66. main(argc,argv)
  67. int argc;
  68. char *argv[];
  69. {
  70.  
  71.     if (argc < 4)
  72.         gripe("if: bad syntax\n");
  73.     argv[argc] = NULL;        /* null terminate for execv */
  74.  
  75.     if (strcmp(strlower(argv[1]),"-r") == 0) {
  76.         if (fstat(argv[2]) != NOTFOUND)    /* it exists, it's readable */
  77.             execv(argv[3],&argv[4]);
  78.         else doshell();
  79.     }
  80.     else if (strcmp(strlower(argv[1]),"!(-r)") == 0) {
  81.         if (fstat(argv[2]) == NOTFOUND)    /* not found */
  82.             execv(argv[3],&argv[4]);
  83.         else doshell();
  84.     }
  85.     else if (strcmp(strlower(argv[1]),"-w") == 0) {
  86.         if (fstat(argv[2]) == READWRITE)    /* rw */
  87.             execv(argv[3],&argv[4]);
  88.         else doshell();
  89.     }
  90.     else if (strcmp(strlower(argv[1]),"!(-w)") == 0) { 
  91.         if (fstat(argv[2]) != READWRITE)    /* readonly */
  92.             execv(argv[3],&argv[4]);
  93.         else doshell();
  94.     }
  95.     else if (strcmp(argv[2],"==") == 0) {
  96.         if (strcmp(strlower(argv[1]),strlower(argv[3])) == 0)
  97.             execv(argv[4],&argv[5]);
  98.         else doshell();
  99.     }
  100.     else if (strcmp(argv[2],"!=") == 0) {
  101.         if (strcmp(strlower(argv[1]),strlower(argv[3])) != 0)
  102.             execv(argv[4],&argv[5]);
  103.         else doshell();
  104.     }
  105.     else gripe("if: unknown condition\n");
  106. }
  107.  
  108. fstat(filnam)
  109. char *filnam;
  110. {
  111. char *byte, c;
  112. fcb address;
  113.  
  114.     setfcb(address,filnam);
  115.     if ((c=bdos(17,&address)) == 255)
  116.         return NOTFOUND;    /* file not found */
  117.  
  118.     byte = (0x80 + (c*32) + _MBYTE); /* permission byte */
  119.  
  120.     if ((*byte & '\200') == 0)
  121.         return READWRITE;    /* readwrite */
  122.     else return READONLY;        /* readonly */
  123. }
  124.  
  125. doshell()
  126. {
  127.     /*    execl("a:sh",0);    */
  128.     exit(0);
  129. }
  130.  
  131. gripe(string)
  132. char *string;
  133. {
  134.     puts(string);        /* give message */
  135.     doshell();        /* & maybe reinvoke shell */
  136. }
  137.